'use strict';

const semver = require("semver") // 比对版本号
const colors = require("colors/safe")
const log = require("@wss-cli-dev/log")
const LOWEST_NODE_VERSION = "18.0.1"


class Command {
  constructor(argv) {
    if (!argv) {
      throw new Error("参数不能为空！")
    }
    if (!Array.isArray(argv)) {
      throw new Error("参数必须为数组！")
    }
    if (argv.length < 1) {
      throw new Error('参数列表为空！');
    }
    this._argv = argv
    let runner = new Promise((resolve, reject) => {
      let chain = Promise.resolve()
      chain = chain.then(() => this.checkNodeVersion())
      chain = chain.then(() => this.initArgv())
      chain = chain.then(() => this.init())
      chain = chain.then(() => this.exec())
      chain.catch(err => {
        log.error(err.message)
      })
    })
  }
  initArgv() {
    this._cmd = this._argv[this._argv.length - 1]
    this._argv = this._argv.slice(0, this._argv.length - 1)
  }

  //  检查node 版本号 当前项目需要的node版本
  checkNodeVersion() {
    // 获取当前
    const currentVersion = process.version
    // 最低版本
    const lowestVersion = LOWEST_NODE_VERSION
    // 比对最低
    if (!semver.gte(currentVersion, lowestVersion)) {
      throw new Error(colors.red(`imooc-cli 需要安装 v${lowestVersion} 以上版本的 Node.js`))
    }
  }
  init() {
    throw new Error("HJDIHD")
  }
  exec() {
    throw new Error("HJD989898IHD")
  }
}

module.exports = Command
